package com.project.xsf.service.impl;

import com.project.junM.entity.TMember;
import com.project.junM.service.TMemberService;
import com.project.xsf.entity.TAssessAnswer;
import com.project.xsf.entity.TAssessQuestion;
import com.project.xsf.entity.TBodyType;
import com.project.xsf.mapper.TAssessAnswerMapper;
import com.project.xsf.mapper.TAssessQuestionOptionMapper;
import com.project.xsf.mapper.TBodyTypeMapper;
import com.project.xsf.service.TBodyTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@Transactional
@Service
public class TBodyTypeImpl implements TBodyTypeService {
    @Autowired
    private TBodyTypeMapper tBodyTypeMapper;
    @Autowired
    private TAssessQuestionOptionMapper tAssessQuestionOptionMapper;
    @Autowired
    private TAssessAnswerMapper tAssessAnswerMapper;
    @Autowired
    private TMemberService  tMemberService;
    @Override
    public Boolean queryByMemberId(HttpSession session) {
        TMember member = (TMember) session.getAttribute("user");
        TBodyType tBodyType = tBodyTypeMapper.queryByMemberId(member.getId());
        if(tBodyType==null||tBodyType.getEvaluateId()!=null){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public Integer addTBodyType(TAssessQuestion[][] objects, HttpSession session) {
        TMember  tMember= (TMember) session.getAttribute("user");
        TMember member = tMemberService.queryById(tMember.getId());
        session.setAttribute("user",member);

        TBodyType  tBodyType = new TBodyType();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tBodyType.setCreatetime(simpleDateFormat.format(new Date()));
        tBodyType.setTMemberId(member.getId());
        tBodyType.setName(member.getName());

        for(int i=0;i<objects.length;i++){
            Integer markTotal=0;

            TAssessQuestion[] tAssessQuestions=(TAssessQuestion[])objects[i];
            for(int j=0;j<tAssessQuestions.length;j++){
//                System.out.println(tAssessQuestions[j]);
                TAssessQuestion tAssessQuestion = tAssessQuestions[j];
                TAssessAnswer tAssessAnswer = new TAssessAnswer();
                tAssessAnswer.setUserId(member.getId());
                tAssessAnswer.setQuestionId(tAssessQuestion.getId());
//                tAssessAnswer.setAnswerId();
                //设置答案id
                if(tAssessQuestion.getIsRadio().equals("0")){
                    tAssessAnswer.setAnswerId(tAssessQuestion.getSelectOption()+"");
//                    System.out.println(tAssessQuestion.getSelectOption());
//                    System.out.println(tAssessQuestionOptionMapper.selectByPrimaryKey(tAssessQuestion.getSelectOption()));
                    markTotal+=tAssessQuestionOptionMapper.selectByPrimaryKey(tAssessQuestion.getSelectOption()).getMark();
                }else{
                    String options="";
                    for(int k=0;k<tAssessQuestion.getCheckOptions().size();k++){
                        options+=tAssessQuestion.getCheckOptions().get(k);
                        Double mark = Double.parseDouble(tAssessQuestion.getCheckOptions().get(k).toString());
//                    System.out.println(tAssessQuestionOptionMapper.selectByPrimaryKey(mark.intValue()).getMark());
                        markTotal+=tAssessQuestionOptionMapper.selectByPrimaryKey(mark.intValue()).getMark();
                        if(k<tAssessQuestion.getCheckOptions().size()-1){
                            options+=",";
                        }
                    }
                    tAssessAnswer.setAnswerId(options);
                }
                tAssessAnswerMapper.insertSelective(tAssessAnswer);
            }

            if(tAssessQuestions[0].getBankId()==4){
                //阳虚
                tBodyType.setTYadc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==5){
                //阴虚
                tBodyType.setTYidc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==6){
                //气虚
                tBodyType.setTQdc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==7){
                //平和
                tBodyType.setTBc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==8){
                //痰湿
                tBodyType.setTPdc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==9){
                //湿热
                tBodyType.setTDhc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==10){
                //血瘀
                tBodyType.setTBsc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==11){
                //气郁
                tBodyType.setTQsc(markTotal);
            }else if(tAssessQuestions[0].getBankId()==12){
                //特禀质
                tBodyType.setTIsc(markTotal);
            }

        }
        return tBodyTypeMapper.insertSelective(tBodyType);

    }
}
